package cn.smbms.controller;

import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.tools.Constants;
import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @program: smbms02
 * @description:
 * @author: wlg
 * @create: 2021-07-27 09:11:06
 **/
@Controller
@RequestMapping("/provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @RequestMapping("/list")
    public String providerList(
            @RequestParam(value = "queryProName", defaultValue = "") String queryProName,
            @RequestParam(value = "queryProCode", defaultValue = "") String queryProCode,
            Model model
    ) {
        List<Provider> providerList = new ArrayList<Provider>();
        providerList = providerService.getProviderList(queryProName, queryProCode);
        model.addAttribute("providerList", providerList);
        model.addAttribute("queryProName", queryProName);
        model.addAttribute("queryProCode", queryProCode);
        return "providerlist";
    }

    @GetMapping("/view/{proid}")
    public String viewProvider(@PathVariable("proid") String id, Model model) {
        if (!StringUtils.isNullOrEmpty(id)) {
            Provider provider = null;
            provider = providerService.getProviderById(id);
            model.addAttribute("provider", provider);
            return "providerview";
        } else {
            return "error";
        }
    }

    @GetMapping("/tomodify")
    public String toModify(@RequestParam("proid") String id, Model model) {
        if (!StringUtils.isNullOrEmpty(id)) {
            Provider provider = null;
            provider = providerService.getProviderById(id);
            model.addAttribute("provider", provider);
            return "providermodify";
        } else {
            return "error";
        }
    }

    @PostMapping("/domodify")
    public String doModify(Provider provider, HttpSession session) {

        provider.setModifyBy(((User) session.getAttribute(Constants.USER_SESSION)).getId());
        provider.setModifyDate(new Date());
        boolean flag = false;
        flag = providerService.modify(provider);
        if (flag) {
            return "redirect:/provider/list";
        } else {
            return "providermodify";
        }
    }

    @GetMapping("/providerlist")
    @ResponseBody
    public String providerList(){
        List<Provider> providerList = new ArrayList<Provider>();
        providerList = providerService.getProviderList("","");
        return JSONArray.toJSONString(providerList);
    }

    @GetMapping("/delete")
    @ResponseBody
    public String delete(@RequestParam("proid")String id){
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if(!StringUtils.isNullOrEmpty(id)){
            int flag = providerService.deleteProviderById(id);
            if(flag == 0){//删除成功
                resultMap.put("delResult", "true");
            }else if(flag == -1){//删除失败
                resultMap.put("delResult", "false");
            }else if(flag > 0){//该供应商下有订单，不能删除，返回订单数
                resultMap.put("delResult", String.valueOf(flag));
            }
        }else{
            resultMap.put("delResult", "notexit");
        }
        return JSONArray.toJSONString(resultMap);
    }

    @GetMapping("/toadd")
    public String toAdd(){
        return "provideradd";
    }

    @PostMapping("/doadd")
    public String doAdd(HttpSession session,Provider provider){
        provider.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
        provider.setCreationDate(new Date());
        boolean flag = false;
        flag = providerService.add(provider);
        if(flag){
            return "redirect:/provider/list";
        }else{
            return "provideradd";
        }
    }
}
